Core.php

<?php

namespace Tlf\User;

class Core extends \Lia\Compo {

    use \Tlf\User\Activations;
    use \Tlf\User\Loader;
    use \Tlf\User\UserManagement;
    use \Tlf\User\Email;
    use \Tlf\User\Database;
    use \Tlf\User\Role;

    //my interface with Sentinel, so that Sentinel is only called from a single location.
    protected $backend;

    public function onCreate(){
        parent::onCreate();
        $this->backend = new Backend($this);
        $this->backend->connectDb();
        //@TODO verify all required configs are set
        // $package = $this->package;
        // $this->backend->connectDb($package);
    
    }

    public function onStart(){
        parent::onStart();
        $package = $this->package;

        if (($limit=$package->get('DB.createIfBefore'))>$now=time()){
            $this->createDatabase();
        }
        if ($package->get('Debug.emptyDB')){
            $this->emptyDB();
        }
        if ($this->package->get('Site.url')==''){
            $s = $_SERVER;
            $ssl      = ($s['HTTPS']??null) == 'on' ;
            $protocol = 'http'.($ssl ? 's' : '' ).'://';
            $site = strtolower($_SERVER['HTTP_HOST']);
            $this->package->set('Site.url', $protocol.$site);
        }
    }
    
    public function onEmit_Server_willDisplayContent($response){
        if (!$response->useTheme)return;
        $response->content = ''.$this->lia->view('user/theme', ['content'=>$response->content]);
    }

    public function apiBackend_tlf_user_getUserBackend(){
        return $this->backend;
    }

    public function apiGet_tlf_user_getUser(){
        return $this->getLoggedInUser();
    }
    
    //@TODO review static functions on user class for refactor
    public function apiGetRole_tlf_user_getUserRole($roleName){
        $role = $this->backend->getRoleFromSlug($roleName);
        if ($role==null){
            $role = $this->backend->createRole($roleName);
        }
        return $role;
    }

    /** return a PDO instance for the user database
     */
    public function apiGetPdo_tlf_user_getUserPdo(){
        return $this->backend->getPdo();
    }
    
    //
    // Utility functions
    //
 
    /**
     * a VERY lenient (poor) email validation 
     * 
     * @param string $email
     * @return boolean
     */
    public function isEmailValid($email){
        //This fails if @ is the first character or if @ is not in the string at all
        if (strpos($email,'@')==0)return false;
        return true;
    }
    
    public function randomPassword(){
        $password = '';
        for ($i=0;$i<random_int(50,55);$i++){
            $password .= chr(random_int(32, 126));
        }
        return $password;
    }
    

}